package aimsite

import (
	"banzhuan-sys/model"
	"banzhuan-sys/pkg/db"
	"banzhuan-sys/pkg/utils"
	"fmt"
	"github.com/gocolly/colly"
	"gorm.io/gorm"
	"strconv"
	"time"
)

type DianYa struct {
	Title string
	Url   string
}

func (dy *DianYa) Run() (bool, error) {

	var posts []model.Posts
	var all []model.Posts
	post := model.Posts{}
	var count int64
	db.My.Model(&model.Posts{}).Where("cat_id = ?", DIANYA_ID).Count(&count)

	c := colly.NewCollector(
		colly.UserAgent(utils.RandomUA()),
		//colly.MaxDepth(39),
	)

	c.Limit(&colly.LimitRule{
		DomainGlob: "*",
		Delay:      5 * time.Second,
	})

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL)
	})

	c.OnHTML(".post-list", func(e *colly.HTMLElement) {

		posts = make([]model.Posts, 0)

		e.ForEach(".post-item", func(i int, e *colly.HTMLElement) {
			post.Title = e.ChildText("h2[class=post-title]")
			post.CatId = DIANYA_ID
			post.Href, _ = e.DOM.Find(".post-title>a").Attr("href")
			post.Href = DIANYA + post.Href

			if count == 0 {
				posts = append(posts, post)
			}else{
				res := db.My.Where("href = ?", post.Href).First(&model.Posts{})
				if res.Error == gorm.ErrRecordNotFound {
					posts = append(posts, post)
				}else{
					return
				}
			}

		})

		if len(posts) == 0 {
			return
		}

		all = append(all, posts...)

	})

	c.OnHTML("ul li", func(e *colly.HTMLElement) {
		if len(posts) == 0 {
			return
		}
		if e.Attr("class") == " ant-pagination-next" {
			if e.Attr("aria-disabled") == "false" {
				nextUrl := DIANYA + e.ChildAttr("a", "href")
				e.Request.Visit(nextUrl)
			}
		}
	})

	c.OnScraped(func(r *colly.Response) {

		fmt.Println("结束", r.Request.URL)
		page, _ := strconv.Atoi(r.Request.URL.Query().Get("page"))
		if page == 1 {
			for i, j := 0, len(all)-1; i < j; i, j = i+1, j-1 {
				all[i], all[j] = all[j], all[i]
			}
			//编码utf8mb4_general_ci
			db.My.Create(&all)
		}
	})

	c.Visit(DIANYA + "/categories/5/tags/0-0-19?page=1")

	return true, nil

}
